package com.zay.a5_buttons;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.zay.a5_buttons.pojo.User;

public class ZhuCeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_zhuce);
        Button btn_user_zhuce=findViewById(R.id.btn_user_zhuce);
        EditText edit_user_account=findViewById(R.id.edit_user_account);
        EditText edit_user_password=findViewById(R.id.edit_user_password);
        btn_user_zhuce.setOnClickListener(view -> {
            String uAccount=edit_user_account.getText().toString();
            String uPassword=edit_user_password.getText().toString();
            if("".equals(uAccount)||"".equals(uPassword)){
                Toast.makeText(ZhuCeActivity.this,"账号或密码不能为空",Toast.LENGTH_SHORT).show();
            }else {
                User user=new User(uAccount,uPassword);
                long row=insertUser(user);
                Toast.makeText(ZhuCeActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(ZhuCeActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    public long insertUser(User user){
        // 创建DatabaseHelper对象
        MySQLiteOpenHelper dbHelper = new MySQLiteOpenHelper(ZhuCeActivity.this,"user_tab",null,1);
        // 调用getWritableDatabase()方法创建或打开一个可以读的数据库
        SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("uAccount",user.getuAccount());
        values.put("uPassword",user.getuPassword());
        long row = sqLiteDatabase.insert("user_tab",null,values);
        sqLiteDatabase.close();
        return row;
    }
}
